package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.AddressVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAddressService;
import com.platform.util.ApiBaseAction;
import com.platform.util.HttpClientUtils;
import com.platform.util.TFAddressUtils;
import com.platform.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者: @author Harmon <br> 时间: 2017-08-11 08:32<br> 描述: ApiIndexController <br>
 */
@Api(tags = "收货地址")
@RestController
@RequestMapping("/api/address")
public class ApiAddressController extends ApiBaseAction {

  @Autowired
  private ApiAddressService addressService;


  public static final String MAP_SDK_KEY = "HDFBZ-EWTKW-HUNRK-O5N5I-VWYD7-ESFW6";


  /**
   * 获取用户的收货地址
   */
  @ApiOperation(value = "获取用户的收货地址接口", response = Map.class)
  @GetMapping("list")
  public Object list(@LoginUser UserVo loginUser) {
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("user_id", loginUser.getUserId());
    List<AddressVo> addressEntities = addressService.queryList(param);
    return toResponsSuccess(addressEntities);
  }

  /**
   * 获取收货地址的详情
   */
  @ApiOperation(value = "获取收货地址的详情", response = Map.class)
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "收获地址ID", required = true, dataType = "Integer")})
  @GetMapping("detail")
  public Object detail(Integer id, @LoginUser UserVo loginUser) {
    AddressVo entity = addressService.queryObject(id);
    if (entity == null && StringUtils.isNotEmpty(loginUser.getMobile())) {
      AddressVo addressVo = new AddressVo();
      addressVo.setTelNumber(loginUser.getMobile());
      return toResponsSuccess(entity);
    } else if (entity == null && loginUser.getMobile() == null) {
      return toResponsSuccess(new AddressVo());
    }
    //判断越权行为
    if (!entity.getUserId().equals(loginUser.getUserId())) {
      return toResponsObject(403, "您无权查看", "");
    }
    return toResponsSuccess(entity);
  }

  /**
   * 添加或更新收货地址
   */
  @ApiOperation(value = "添加或更新收货地址", response = Map.class)
  @PostMapping("save")
  public Object save(@LoginUser UserVo loginUser) {
    try {
      JSONObject addressJson = this.getJsonRequest();
      AddressVo entity = new AddressVo();

      if (null != addressJson) {
        entity.setId(addressJson.getLong("id"));
        entity.setUserId(loginUser.getUserId());
        entity.setUserName(addressJson.getString("userName"));
        entity.setPostalCode(addressJson.getString("postalCode"));
        entity.setProvinceName(addressJson.getString("provinceName"));
        entity.setCityName(addressJson.getString("cityName"));
        entity.setCountyName(addressJson.getString("countyName"));
        entity.setDetailInfo(addressJson.getString("detailInfo"));
        entity.setNationalCode(addressJson.getString("nationalCode"));
        entity.setTelNumber(addressJson.getString("telNumber"));
        entity.setIs_default(addressJson.getInteger("is_default"));
      }

      //检测配送范围
      String errmsg = addressService.checkDeliveryDistance(entity);
      if (StringUtils.isNotEmpty(errmsg)) {
        return toResponsFail(errmsg);
      }
      if (null == entity.getId() || entity.getId() == 0) {
        entity.setId(null);
        addressService.save(entity);
      } else {
        addressService.update(entity);
      }
      return toResponsSuccess(entity);
    } catch (Exception e) {
      logger.error(e);
      return toResponsFail("配送地址不存在");
    }

  }

  /**
   * 删除指定的收货地址
   */
  @ApiOperation(value = "删除指定的收货地址", response = Map.class)
  @PostMapping("delete")
  public Object delete(@LoginUser UserVo loginUser) {
    JSONObject jsonParam = this.getJsonRequest();
    Integer id = jsonParam.getIntValue("id");

    AddressVo entity = addressService.queryObject(id);
    //判断越权行为
    if (!entity.getUserId().equals(loginUser.getUserId())) {
      return toResponsObject(403, "您无权删除", "");
    }
    addressService.delete(id);
    return toResponsSuccess("");
  }

  /**
   * 获取用户的收货地址
   */
  @IgnoreAuth
  @ApiOperation(value = "获取用户的收货地址接口", response = Map.class)
  @GetMapping("addressUserlist")
  public Object addressUserlist(@LoginUser UserVo loginUser) {
    Map<String, Object> param = new HashMap<String, Object>();
    param.put("user_id", this.getUserId().intValue());
    List<AddressVo> addressEntities = addressService.queryaddressUserlist(param);
    return toResponsSuccess(addressEntities);
  }

}